package com.ibm.cps.sql.expression;

import java.util.Map;

public class MapBasedFieldEvaluator implements FieldEvaluator {
	private final Map<String, Object> valueMap;
	private final Object[] values;

	public MapBasedFieldEvaluator(Map<String, Object> valueMap) {
		this.valueMap = valueMap;
		this.values = flattenValues(valueMap);
	}
	
	private Object[] flattenValues(Map<String, Object> valueMap){
		Object[] values = new Object[valueMap.size()];
		int index = 0;
		for (String key : valueMap.keySet()) {
			values[index] = valueMap.get(key);
			index++;
		}
		return values;
	}

	@Override
	public Object evaluate(Object fieldName) {
		return valueMap.get(fieldName);
	}

	@Override
	public Object evaluate(int index) {
		return values[index];
	}

	@Override
	public Object[] evaluateasArray(Object fieldName) {
		return (Object[]) valueMap.get(fieldName);
	}

}
